<?php


namespace app\api\model;


use think\Model;

class ShortLink extends Model
{
    protected $pk = 'link_id';

    public static function existLinkByUrl($url)
    {
        return self::where('link_url', $url)
            ->where('expire_time', '>', time())
            ->find();
    }

    public static function existCode($code)
    {
        return self::where('link_code', $code)->find();
    }

    public static function generateShortLink($code, $url, $expire)
    {
        return ShortLink::create([
            'link_code' => $code,
            'link_url' => $url,
            'expire_time' => time() + $expire * 86400,
        ]);
    }
}